package com.study.utils;

import com.alibaba.druid.pool.DruidPooledConnection;

import java.sql.SQLException;

/**
 * @author Qi XueSong
 */
public class CollectionUtils {

    // 使用 IOC 的方式，所以要去掉私有构造器，不然反射的时候没办法获取到
    /*private CollectionUtils(){}

    private final static CollectionUtils collectionUtils = new CollectionUtils();

    public CollectionUtils getInstance(){
        return collectionUtils;
    }*/

    //存储当前线程的连接
    private final ThreadLocal<DruidPooledConnection> threadLocal = new ThreadLocal<>();

    /**
     * 获取连接
     */
    public DruidPooledConnection getConnection() throws SQLException {
        // 获取当前线程中的连接
        DruidPooledConnection druidPooledConnection = threadLocal.get();
        //判断当前线程中是否存在连接
        if(druidPooledConnection == null){
            //不存在，则从工具类中获取连接
            druidPooledConnection = DruidUtils.getInstance().getConnection();
            //将连接放到当前线程中
            threadLocal.set(druidPooledConnection);
        }
        return druidPooledConnection;
    }

    private void initMethod(){
        System.out.println("初始化方法");
    }

    private void destroyMethod(){
        System.out.println("销毁方法");
    }
}
